package com.keshe.config;
//
//import com.keshe.security.SpringSecurityUserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
//import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.builders.WebSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.security.web.SecurityFilterChain;
//
///*
// * Security配置类
// */
//@EnableMethodSecurity(
//        prePostEnabled = true
//)
//@Configuration
//@EnableWebSecurity
//public class SecurityConfiguration {
//
//    @Autowired
//    private SpringSecurityUserService userService;
//
//    @Bean
//    protected PasswordEncoder myPasswordEncoder() {
//        return new BCryptPasswordEncoder();
//    }
//
//    @Bean
//    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
//        http
//                .csrf(csrf -> csrf.disable()) // 使用新的配置方式禁用CSRF
//                .authorizeHttpRequests(authorize -> authorize
//                        .requestMatchers("/webapp/login.html").permitAll()
//                        .anyRequest().authenticated())
//                .formLogin(form -> form
//                        .loginPage("/webapp/login.html") // 自定义登录页面
//                        .loginProcessingUrl("/user/login") // 登录处理Url
//                        .usernameParameter("username").passwordParameter("password")// 修改自定义表单name值.
//                        .defaultSuccessUrl("/webapp/main.html")) // 登录成功后跳转路径
//                .headers(headers -> headers
//                        .frameOptions(frameOptions -> frameOptions.disable())
//                );
//
//
//        return http.build();
//    }
//    //释放jss
//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().requestMatchers("/webapp/lib/**","/webapp/static/**");
//    }
//
//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return new BCryptPasswordEncoder();
//    }
//
//    @Autowired
//    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
//    }
//}
//



import com.keshe.security.SpringSecurityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/*
 * Security配置类
 */
@EnableMethodSecurity(
        prePostEnabled = true
)
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    /*
     * http请求处理方法
     *
     * @param http
     * @throws Exception
     */


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .formLogin()// 开启表单认证
                .loginPage("/webapp/login.html")// 自定义登录页面
                .loginProcessingUrl("/user/login")// 登录处理Url
                .usernameParameter("username").passwordParameter("password")// 修改自定义表单name值.// 修改自定义表单name值.
                .defaultSuccessUrl("/webapp/index.html")// 登录成功后跳转路径
                .and()
                .authorizeRequests()
                .antMatchers("/webapp/login.html").permitAll()
                .antMatchers("/captcha/generateCaptcha").permitAll()
                .anyRequest().authenticated(); //所有请求都需要登录认证才能访问;
        // 关闭csrf防护
        http.csrf().disable();
        // 允许iframe加载页面
        http.headers().frameOptions().sameOrigin();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/webapp/lib/**","/webapp/static/**");
    }

    @Autowired
    private SpringSecurityUserService  userService;
    @Bean
    protected PasswordEncoder myPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(myPasswordEncoder());
    }
    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("admin"));
    }
}
